package com.nbcio.cd.controller;

import com.nbcio.cd.domain.CdPermission;
import com.nbcio.cd.domain.vo.CdPermissionVo;
import com.nbcio.cd.service.ICdPermissionService;
import com.nbcio.common.core.domain.R;
import com.nbcio.common.core.utils.MapstructUtils;
import com.nbcio.common.core.utils.poi.ExcelUtil;
import com.nbcio.common.web.core.BaseController;
import com.nbcio.common.log.annotation.Log;
import com.nbcio.common.log.enums.BusinessType;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 云网盘权限管理Controller
 *
 * @author nbacheng
 * @date 2024-01-01
 */
@RestController
@RequestMapping("/cd/permission")
public class CdPermissionController extends BaseController {

    @Autowired
    private ICdPermissionService cdPermissionService;

    /**
     * 查询云网盘权限列表
     */
    @SaCheckPermission("cd:permission:list")
    @GetMapping("/list")
    public R<List<CdPermissionVo>> list(CdPermission cdPermission) {
        List<CdPermissionVo> list = cdPermissionService.selectCdPermissionList(cdPermission);
        return R.ok(list);
    }

    /**
     * 导出云网盘权限列表
     */
    @SaCheckPermission("cd:permission:export")
    @Log(title = "云网盘权限", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CdPermission cdPermission) {
        List<CdPermissionVo> list = cdPermissionService.selectCdPermissionList(cdPermission);
        ExcelUtil.exportExcel(list, "云网盘权限数据", CdPermissionVo.class, response);
    }

    /**
     * 获取云网盘权限详细信息
     */
    @SaCheckPermission("cd:permission:query")
    @GetMapping(value = "/{permissionId}")
    public R<CdPermissionVo> getInfo(@PathVariable("permissionId") Long permissionId) {
        CdPermission permission = cdPermissionService.getById(permissionId);
        return R.ok(MapstructUtils.convert(permission, CdPermissionVo.class));
    }

    /**
     * 新增云网盘权限
     */
    @SaCheckPermission("cd:permission:add")
    @Log(title = "云网盘权限", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@RequestBody CdPermission cdPermission) {
        return toAjax(cdPermissionService.insertCdPermission(cdPermission));
    }

    /**
     * 修改云网盘权限
     */
    @SaCheckPermission("cd:permission:edit")
    @Log(title = "云网盘权限", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@RequestBody CdPermission cdPermission) {
        return toAjax(cdPermissionService.updateCdPermission(cdPermission));
    }

    /**
     * 删除云网盘权限
     */
    @SaCheckPermission("cd:permission:remove")
    @Log(title = "云网盘权限", businessType = BusinessType.DELETE)
    @DeleteMapping("/{permissionIds}")
    public R<Void> remove(@PathVariable Long[] permissionIds) {
        return toAjax(cdPermissionService.deleteCdPermissionByPermissionIds(permissionIds));
    }

    /**
     * 检查用户权限
     */
    @GetMapping("/checkUserPermission")
    public R<Boolean> checkUserPermission(@RequestParam Long userId,
                                          @RequestParam String resourceType,
                                          @RequestParam Long resourceId,
                                          @RequestParam String permissionType) {
        boolean hasPermission = cdPermissionService.checkUserPermission(userId, resourceType, resourceId, permissionType);
        return R.ok(hasPermission);
    }

    /**
     * 检查角色权限
     */
    @GetMapping("/checkRolePermission")
    public R<Boolean> checkRolePermission(@RequestParam Long roleId,
                                         @RequestParam String resourceType,
                                         @RequestParam Long resourceId,
                                         @RequestParam String permissionType) {
        boolean hasPermission = cdPermissionService.checkRolePermission(roleId, resourceType, resourceId, permissionType);
        return R.ok(hasPermission);
    }

    /**
     * 获取用户权限列表
     */
    @GetMapping("/getUserPermissions")
    public R<List<String>> getUserPermissions(@RequestParam Long userId,
                                              @RequestParam String resourceType,
                                              @RequestParam Long resourceId) {
        List<String> permissions = cdPermissionService.getUserPermissions(userId, resourceType, resourceId);
        return R.ok(permissions);
    }

    /**
     * 获取角色权限列表
     */
    @GetMapping("/getRolePermissions")
    public R<List<String>> getRolePermissions(@RequestParam Long roleId,
                                             @RequestParam String resourceType,
                                             @RequestParam Long resourceId) {
        List<String> permissions = cdPermissionService.getRolePermissions(roleId, resourceType, resourceId);
        return R.ok(permissions);
    }

    /**
     * 为用户授权
     */
    @SaCheckPermission("cd:permission:grant")
    @Log(title = "云网盘权限", businessType = BusinessType.GRANT)
    @PostMapping("/grantUserPermission")
    public R<Void> grantUserPermission(@RequestParam Long userId,
                                         @RequestParam String resourceType,
                                         @RequestParam Long resourceId,
                                         @RequestBody List<String> permissionTypes) {
        int result = cdPermissionService.grantUserPermission(userId, resourceType, resourceId, permissionTypes);
        return toAjax(result);
    }

    /**
     * 为角色授权
     */
    @SaCheckPermission("cd:permission:grant")
    @Log(title = "云网盘权限", businessType = BusinessType.GRANT)
    @PostMapping("/grantRolePermission")
    public R<Void> grantRolePermission(@RequestParam Long roleId,
                                         @RequestParam String resourceType,
                                         @RequestParam Long resourceId,
                                         @RequestBody List<String> permissionTypes) {
        int result = cdPermissionService.grantRolePermission(roleId, resourceType, resourceId, permissionTypes);
        return toAjax(result);
    }

    /**
     * 撤销用户权限
     */
    @SaCheckPermission("cd:permission:revoke")
    @Log(title = "云网盘权限", businessType = BusinessType.DELETE)
    @PostMapping("/revokeUserPermission")
    public R<Void> revokeUserPermission(@RequestParam Long userId,
                                          @RequestParam String resourceType,
                                          @RequestParam Long resourceId,
                                          @RequestBody List<String> permissionTypes) {
        int result = cdPermissionService.revokeUserPermission(userId, resourceType, resourceId, permissionTypes);
        return toAjax(result);
    }

    /**
     * 撤销角色权限
     */
    @SaCheckPermission("cd:permission:revoke")
    @Log(title = "云网盘权限", businessType = BusinessType.DELETE)
    @PostMapping("/revokeRolePermission")
    public R<Void> revokeRolePermission(@RequestParam Long roleId,
                                          @RequestParam String resourceType,
                                          @RequestParam Long resourceId,
                                          @RequestBody List<String> permissionTypes) {
        int result = cdPermissionService.revokeRolePermission(roleId, resourceType, resourceId, permissionTypes);
        return toAjax(result);
    }

    /**
     * 获取资源权限列表
     */
    @SaCheckPermission("cd:permission:list")
    @GetMapping("/getResourcePermissions")
    public R<List<CdPermissionVo>> getResourcePermissions(@RequestParam String resourceType,
                                                          @RequestParam Long resourceId) {
        List<CdPermissionVo> permissions = cdPermissionService.getResourcePermissions(resourceType, resourceId);
        return R.ok(permissions);
    }

    /**
     * 复制权限
     */
    @SaCheckPermission("cd:permission:copy")
    @Log(title = "云网盘权限", businessType = BusinessType.OTHER)
    @PostMapping("/copyPermissions")
    public R<Void> copyPermissions(@RequestParam String sourceResourceType,
                                     @RequestParam Long sourceResourceId,
                                     @RequestParam String targetResourceType,
                                     @RequestParam Long targetResourceId) {
        int result = cdPermissionService.copyPermissions(sourceResourceType, sourceResourceId, 
                                                        targetResourceType, targetResourceId);
        return toAjax(result);
    }

    /**
     * 清理无效权限
     */
    @SaCheckPermission("cd:permission:clean")
    @Log(title = "云网盘权限", businessType = BusinessType.CLEAN)
    @PostMapping("/cleanInvalidPermissions")
    public R<Void> cleanInvalidPermissions() {
        int result = cdPermissionService.cleanInvalidPermissions();
        return toAjax(result);
    }
}
